﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

using SmartParking.Data.Entities;
using SmartParking.Bussiness;

namespace SmartParking.Web.Manager
{
    public partial class Log : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                (Master as Logger).Title = "Log";
                (Master as Logger).ActiveMenuItem = "Log";
                LoadLog();
            }
        }

        private void LoadLog()
        {
            LogManager.BindLog(ref gvLog);

            LogManager.BindIPAddresses(ref ddlIPAddress);
            ddlIPAddress.Items.Insert(0, new ListItem("Select one", string.Empty));

            LogManager.BindManagers(ref ddlManager);
            ddlManager.Items.Insert(0, new ListItem("Select one", "-1"));

            LogManager.BindTables(ref ddlTable);
            ddlTable.Items.Insert(0, new ListItem("Select one", string.Empty));

            ddlAction.Items.Insert(0, new ListItem("Select one", string.Empty));
        }

        protected void gvLog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblManager = (Label)e.Row.Cells[0].FindControl("lblManager");
                HiddenField hfIdManager = (HiddenField)e.Row.Cells[0].FindControl("hfIdManager");

                if (lblManager != null)
                {
                    SmartParking.Data.Entities.Manager manager = SecurityManager.GetManager(Convert.ToInt32(hfIdManager.Value));
                    if (manager != null)
                        lblManager.Text = manager.UserName;
                }
            }
        }

        protected void gvLog_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvLog.PageIndex = e.NewPageIndex;
            LoadReport();
        }

        protected void btnRun_Click(object sender, EventArgs e)
        {
            gvLog.PageIndex = 0;
            LoadReport();
        }

        private void LoadReport()
        {
            string ipAddress;
            string action;
            string table;
            int manager;
            Nullable<DateTime> fromDate = null;
            Nullable<DateTime> toDate = null;

            if (ddlAction.SelectedValue != string.Empty)
                action = ddlAction.SelectedValue;
            else
                action = string.Empty;

            if (ddlIPAddress.SelectedValue != string.Empty)
                ipAddress = ddlIPAddress.SelectedValue;
            else
                ipAddress = string.Empty;

            if (ddlManager.SelectedValue != "-1")
                manager = Convert.ToInt32(ddlManager.SelectedValue);
            else
                manager = -1;

            if (ddlTable.SelectedValue != string.Empty)
                table = ddlTable.SelectedValue;
            else
                table = string.Empty;

            if (txtFromDate.Text != string.Empty)
                fromDate = Convert.ToDateTime(txtFromDate.Text);

            if (txtToDate.Text != string.Empty)
                toDate = Convert.ToDateTime(txtToDate.Text);

            LogManager.BindLog(ref gvLog, manager, table, action, ipAddress, fromDate, toDate);
        }
    }
}
